# from modelscope.pipelines import pipeline
# from modelscope.utils.constant import Tasks
import os
import shutil
from PIL import Image
from tqdm import tqdm 
import glob
# def main():
#     table_recognition = pipeline(Tasks.table_recognition, model='/data/fengyy/project/CycleCenterNet/cv_dla34_table-structure-recognition_cycle-centernet')
#     result = table_recognition('/data/fengyy/dateset/temp/0429/origin_5-0003_0017.jpg')
#     print(result)


def test():
    data_dir = r"/data/fengyy/dateset/temp/0429/result/模板二_001"
    save_dir = r"/data/fengyy/dateset/temp/0429/image_source/模板二_001"
    for root,dirs,files in tqdm(os.walk(data_dir)):
        for file_name in files:
            if os.path.splitext(file_name)[1] == ".jpg" and "origin_" in file_name:
                path = os.path.join(root,file_name)
                shutil.copyfile(path,os.path.join(save_dir,file_name))
    return


def test2():
    data_dir = r"/data/fengyy/dateset/temp/0429/image_source/模板二_001"
    save_dir = r"/data/fengyy/dateset/temp/0429/image_source/result"
    size = 500
    for file_name in tqdm(os.listdir(data_dir)):
        input_image_path = os.path.join(data_dir,file_name)
        output_image_path = os.path.join(save_dir,file_name)
        with Image.open(input_image_path) as image:
            (original_width, original_height) = image.size
            new_width = size
            new_height = size * original_height / original_width
    
            resized_image = image.resize((int(new_width), int(new_height)), Image.ANTIALIAS)
            resized_image.save(output_image_path)
    return


def test3():
    data_dir = r"/data/fengyy/dateset/temp/0429/result/模板一_001"
    save_dir = r"/data/fengyy/dateset/temp/0429/image_source/模板一"
    print(data_dir)
    for root,dirs,files in tqdm(os.walk(data_dir)):
        print(root)
        for file_name in files:
            if os.path.splitext(file_name)[1] == ".jpg" and "origin_" in file_name and "_001" in file_name:
                path = os.path.join(root,file_name)
                print(path)
                shutil.copyfile(path,os.path.join(save_dir,file_name))
    return


def test4():
    # save_dir = r""
    # for name in os.listdir(save_dir):
    #     sub_dir = os.path.join(save_dir,name)
    #     for file_name in os.listdir(sub_dir):
    #         file_path  = os.path.join(sub_dir,file_name)
    # paths = glob.glob(r"/data/fengyy/guiji_Data/guiji_all_1/*/shape2line_buffer_Eliminate.*")
    # save_dir = r"/data/fengyy/guiji_Data/shp"
    # paths = glob.glob(r"/data/fengyy/guiji_Data/jianzhu/*/shape2line_buffer_Eliminate.cpg")
    # save_dir = r"/data/fengyy/guiji_Data/jianzhucopy"
    
    # re_path = r"/data/fengyy/guiji_Data/guiji_all_2/*/shape2line_buffer_Eliminate.cpg"
    re_path = r"/data/fengyy/guiji_Data/result/*.shx"
    paths = glob.glob(re_path)
    save_dir = r"/data/fengyy/guiji_Data/guiji_all_2_fyy_1"
    
    for path in paths:
        dir_path,file_name = os.path.split(path)
        name, file_ext = os.path.splitext(file_name)
        dir_name = os.path.basename(dir_path)
        file_name = "shape2line_buffer_Eliminate"
        npath = os.path.join(save_dir,name,f"{file_name}{file_ext}")
        shutil.copyfile(path,npath)
    return


if __name__ == "__main__":
    test4()